﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.MEP
{
	[Transaction(TransactionMode.Manual)]
	public class HyGPSRangeCheck : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = null;
			Selection selection = activeUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(selection);
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(1);
			if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				ProtectRangeAddMannerForm protectRangeAddMannerForm = new ProtectRangeAddMannerForm();
				DialogResult dialogResult = protectRangeAddMannerForm.ShowDialog();
				if (dialogResult == DialogResult.Cancel)
				{
					protectRangeAddMannerForm.Close();
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					CreateProtectRange createProtectRange = new CreateProtectRange();
					IList<Element> collection = new List<Element>();
					bool flag = true;
					while (flag)
					{
						transaction = new Transaction(document, "创建保护范围");
						transaction.Start();
						switch (protectRangeAddMannerForm.SelectAddManner)
						{
						case AddManner.ManualAdd:
						{
                            Autodesk.Revit.DB.View view = document.ActiveView;
							collection = createProtectRange.GeteElementByRectangle(document, this.rangeCheckApplicatScenarios);
							createProtectRange.CreatDetailCurve(document, view, collection, this.rangeCheckApplicatScenarios);
							break;
						}
						case AddManner.ActiveViewAutoAdd:
						{
                            Autodesk.Revit.DB.View view = document.ActiveView;
							collection = createProtectRange.GetElementByView(document, view);
							createProtectRange.CreatDetailCurve(document, view, collection, this.rangeCheckApplicatScenarios);
							flag = false;
							break;
						}
						case AddManner.AllModelAutoAdd:
						{
                            IList<Autodesk.Revit.DB.View> floorPlanViews = createProtectRange.GetFloorPlanViews(document);
                            foreach (Autodesk.Revit.DB.View view2 in floorPlanViews)
							{
                                Autodesk.Revit.DB.View view = view2;
								IList<Element> elementByView = createProtectRange.GetElementByView(document, view);
								createProtectRange.CreatDetailCurve(document, view, elementByView, this.rangeCheckApplicatScenarios);
							}
							flag = false;
							break;
						}
						}
						transaction.Commit();
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
                if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private HyRangeCheckMajor rangeCheckApplicatScenarios;
	}
}
